package com.test.daily.leetcode.y2022.m06.day0619.v01;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/6/19 上午 08:38
 * @version: 1.0
 */
public class Solution {
    public static class Node {
        public Node left;
        public Node right;
        public int value;

        public Node(int value) {
            this.value = value;
        }
    }

    public static void morris(Node head) {
        Node cur = head;
        Node mostRightest = null;
        while (cur != null) {
            mostRightest = cur.left;
            if (mostRightest != null) {
                while (mostRightest.right != null && mostRightest.right != cur) {
                    mostRightest = mostRightest.right;
                }
                if (mostRightest.right == null) {
                    System.out.print(" " + cur.value);
                    mostRightest.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    mostRightest.right = null;
                }
            } else {
                System.out.print(" " + cur.value);
            }
            cur = cur.right;
        }
    }
}
